/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.servlet;

import cn.webrx.db.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>Project: jdbc202202 - CheckServlet
 * <p>Powered by webrx On 2022-08-23 14:24:50
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

@WebServlet("/check")
public class CheckServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String a = req.getParameter("account");
        String p = req.getParameter("password");

        DbUtil du = new DbUtil();
        int rows = du.check("select count(*) from t_student where account=? and password=?",a,p);
        if(rows>=1){
            //System.out.println("登录成功");
            //使用session对象
            req.getSession().setAttribute("user",a);
            resp.sendRedirect("/show");
        }else{
            //System.out.println("失败，请检查账号");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<meta charset=\"utf-8\">");
            out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
            out.println("<meta name=\"keywords\" content=\"关键字\">");
            out.println("<meta name=\"description\" content=\"简介\">");
            out.println("</head>");
            out.println("<body>");

            out.println("<script>");
            out.println("alert('登录失败，请检查账号密码');location.href = '/login.jsp'");
            out.println("</script>");

            out.println("</body>");
            out.println("</html>");
            out.flush();
            out.close();
        }

    }
}
